package uit.bus;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class TimKiem extends Activity implements OnClickListener,
		OnCheckedChangeListener {
	public AutoCompleteTextView AutoCompleteDi;
	public AutoCompleteTextView AutoCompleteDen;
	public Button btTimKiem;
	public Button btDaoNguoc;
	public CheckBox cbDen;
	public String[] place;
	public ArrayAdapter<String> adapter;
	public DBAdapter db;
	public String strDi = "";
	public String strDen = "";
	public SharedPrefren share;
	public SharedPreferences sharePre;
	public double longt, lat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timkiem);
		AutoCompleteDen = (AutoCompleteTextView) findViewById(R.id.AutoCompleteDen);
		AutoCompleteDi = (AutoCompleteTextView) findViewById(R.id.AutoCompleteDi);
		cbDen = (CheckBox) findViewById(R.id.cbDen);
		btTimKiem = (Button) findViewById(R.id.btTimKiem);
		btDaoNguoc = (Button) findViewById(R.id.btDaoNguoc);
		btTimKiem.setOnClickListener(this);
		btDaoNguoc.setOnClickListener(this);
		cbDen.setOnCheckedChangeListener(this);

		getListPlace();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, place);
		AutoCompleteDi.setThreshold(2);
		AutoCompleteDen.setThreshold(2);
		AutoCompleteDi.setAdapter(adapter);
		AutoCompleteDen.setAdapter(adapter);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			String tramdi = bundle.getString("tramdi");
			AutoCompleteDi.setText(tramdi);
			AutoCompleteDi.setFocusable(false);
			AutoCompleteDen.setFocusable(true);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btTimKiem:
			strDi = AutoCompleteDi.getText().toString().trim();
			strDen = AutoCompleteDen.getText().toString().trim();
			if (strDi.equals("")) {
				Toast.makeText(getBaseContext(), "Nơi đi trống...",
						Toast.LENGTH_LONG).show();
			} else {
				if (strDen.equals("")) {
					Toast.makeText(getBaseContext(), "Nơi đến trống...",
							Toast.LENGTH_LONG).show();
				} else {
					if (strDi.equals(strDen)) {
						Toast.makeText(getBaseContext(),
								"Nơi đi trùng với nơi đến...",
								Toast.LENGTH_LONG).show();
					} else {
						// thuc hien tim kiem
						if (checkNoiDi(strDi) && checkNoiDen(strDen)) {
							Intent i = new Intent("uit.bus.KQTIMKIEM");
							Bundle extras = new Bundle();
							extras.putString("noidi", strDi);
							extras.putString("noiden", strDen);
							i.putExtras(extras);
							startActivity(i);
						}
					}
				}
			}
			break;

		case R.id.btDaoNguoc:
			strDi = AutoCompleteDi.getText().toString().trim();
			strDen = AutoCompleteDen.getText().toString().trim();
			if (strDi.equals("")) {
				Toast.makeText(getBaseContext(), "Nơi đi trống...",
						Toast.LENGTH_LONG).show();
			} else {
				if (strDen.equals("")) {
					Toast.makeText(getBaseContext(), "Nơi đến trống...",
							Toast.LENGTH_LONG).show();
				} else {
					AutoCompleteDi.setText(strDen);
					AutoCompleteDen.setText(strDi);
				}
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.cbDen:
			strDi = AutoCompleteDi.getText().toString().trim();
			strDen = AutoCompleteDen.getText().toString().trim();
			if (checkNoiDi(strDi) && checkNoiDen(strDen)) {
				if (!strDi.equals(strDen)) {
					// nhac nho
					sharePre = HCMBusActivity.getSharePred();
					share = new SharedPrefren(sharePre);
					if (isChecked) {
						getLatLongDD(strDen);
						share.setNhacNho(isChecked);
						share.setViTriNN(String.valueOf(lat),
								String.valueOf(longt));
						share.setTenTram(strDen);
					}
				} else {
					Toast.makeText(getBaseContext(),
							"Nơi đi trùng với nơi đến...", Toast.LENGTH_LONG)
							.show();
					cbDen.setChecked(false);
				}
			} else {
				cbDen.setChecked(false);
			}
			break;

		default:
			break;
		}
	}

	public void getListPlace() {
		db = new DBAdapter(this);
		db.open();
		Cursor myCursor = db.getListTram();
		place = new String[myCursor.getCount()];
		// Toast.makeText(getBaseContext(),
		// "so tram =" + String.valueOf(myCursor.getCount()),
		// Toast.LENGTH_LONG).show();
		int index = 0;
		if (myCursor.moveToFirst()) {
			do {
				place[index] = myCursor.getString(0);
				index++;
			} while (myCursor.moveToNext());
		}
		db.close();
	}

	public boolean checkNoiDi(String noidi) {
		boolean kq = false;
		if (!noidi.equals("")) {
			db = new DBAdapter(this);
			db.open();
			kq = db.CheckDiaDiem(noidi);
			if (!kq) {
				Toast.makeText(
						getBaseContext(),
						"Nơi đi không tồn tại trong cơ sở dữ liệu, vui lòng nhập lại...",
						Toast.LENGTH_LONG).show();
			}
			db.close();
		} else {
			Toast.makeText(getBaseContext(),
					"Nơi đi trống, vui lòng nhập lại...", Toast.LENGTH_LONG)
					.show();
		}
		return kq;
	}

	public boolean checkNoiDen(String noiden) {
		boolean kq = false;
		if (!noiden.equals("")) {
			db = new DBAdapter(this);
			db.open();
			kq = db.CheckDiaDiem(noiden);
			if (!kq) {
				Toast.makeText(
						getBaseContext(),
						"Nơi đến không tồn tại trong cơ sở dữ liệu, vui lòng nhập lại...",
						Toast.LENGTH_LONG).show();
			}
			db.close();
		} else {
			Toast.makeText(getBaseContext(),
					"Nơi đến trống, vui lòng nhập lại...", Toast.LENGTH_LONG)
					.show();
		}
		return kq;
	}

	public void getLatLongDD(String diadiem) {
		db = new DBAdapter(this);
		db.open();
		Cursor mycursor = db.getLatLongDD(diadiem);
		if (mycursor.moveToFirst()) {
			lat = mycursor.getDouble(1);
			longt = mycursor.getDouble(2);
		}
		db.close();
	}
}
